<template>
  <!-- 树形控件 -->
  <div>
    <Row style="margin:10px 0;">
      <i-input icon="ios-search" style="width:100%;" v-model="title" :placeholder="placeholder"></i-input>
    </Row>

    <Tree
      :data="baseDataList"
      :show-checkbox="showCheckbox"
      :check-strictly="true"
      @on-select-change="checkNode"
      @on-check-change="checkNodes"
    ></Tree>
  </div>
</template>

<script>
import util from "@/libs/util.js";
import { selectOfEnergyArchivesTree } from "@/api/efficiency.js";
export default {
  props: {
    showCheckbox: {
      type: Boolean,
      default: false
    },
    checkNode: {
      type: Function,
      default: null
    },
    checkNodes: {
      type: Function,
      default: null
    },
    placeholder: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      baseDataList: [],
      title: "",
      checkList: []
    };
  },
  computed: {},
  watch: {
    title(val) {
      selectOfEnergyArchivesTree({
        custId: this.token.custId,
        nodeName: val
      }).then(res => {
        if (res.code == 200) {
          let baseData = JSON.parse(
            JSON.stringify(res.result).replace(/nodeName/g, "title")
          );
          baseData = JSON.parse(
            JSON.stringify(baseData).replace(/nodeName/g, "energyUnitName")
          );
          baseData = this.filterId(baseData);
          this.baseDataList = baseData;
        }
      });
    }
  },
  methods: {
    filterId(list) {
      list.map(res => {
        res.expand = true;
        if (
          "energyUnitId" in res &&
          res.energyUnitId != null &&
          res.energyUnitId != "null"
        ) {
          res.disableCheckbox = false;
        } else {
          res.disableCheckbox = true;
        }

        if (
          "children" in res &&
          res.children != null &&
          res.children.length > 0
        ) {
          this.filterId(res.children);
        }
      });
      return list;
    },
    getId(list) {
      list.map(res => {
        if ("children" in res && res.children != null) {
          this.getId(res.children);
        }
        if ("checked" in res && res.checked == true) {
          this.checkList.push(res);
        }
      });
    }
  },
  created() {
    let token = util.token.initToken();
    this.token = token;
    selectOfEnergyArchivesTree({ custId: token.custId, nodeName: "" }).then(
      res => {
        if (res.code == 200) {
          let baseData = JSON.parse(
            JSON.stringify(res.result).replace(/nodeName/g, "title")
          );
          baseData = JSON.parse(
            JSON.stringify(baseData).replace(/nodeName/g, "energyUnitName")
          );
          baseData = this.filterId(baseData);
          this.baseDataList = baseData;
        }
      }
    );
  },
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {} // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>